package com.rzico.sbl.ui.report.manager

import android.widget.ImageView
import android.widget.TextView
import androidx.core.view.isVisible
import com.lzy.okgo.model.HttpMethod
import com.rzico.sbl.R
import com.rzico.sbl.databinding.ActivityReportStationListBinding
import com.rzico.sbl.model.ManagerStation
import com.rzico.sbl.other.getBeforeDate
import com.rzico.sbl.other.getEndDate
import com.rzico.sbl.other.getFirstDate
import com.rzico.sbl.other.getTodayDate
import com.rzico.sbl.ui.report.other.ReportUrl
import com.rzico.sbl.viewmodel.ReportStationViewModel
import com.xinnuo.common.extend.*
import com.xinnuo.common.decoration.SpaceTBDecoration
import com.xinnuo.common_ui.base.*
import com.xinnuo.common_ui.glideExt.loadCircleImage
import com.xinnuo.common_ui.utils.showActionDialog
import com.xinnuo.slimadapter.SlimAdapter
import org.json.JSONObject

class ReportStationListActivity : BaseActivity(R.layout.activity_report_station_list) {

    private val mBinding by lazy { ActivityReportStationListBinding.bind(getRootView()) }

    private lateinit var mBeginDate: String
    private lateinit var mEndDate: String

    override fun getViewModel() = get<ReportStationViewModel>()

    override fun initData() {
        initTitle(title = "水站列表", intent.getExtra("dateHint"))

        mBeginDate = intent.getExtra("beginDate")
        mEndDate = intent.getExtra("endDate")

        initLayout()

        mBinding
            .listLayout
            .swipeRefresh
            .isRefreshing = true

        getTotalData()
        getData(pageNum)
    }

    private fun initLayout() {
        mBinding.listLayout.apply {
            emptyLayout.also {
                it.emptyHint.text = "暂无相关水站信息！"
            }

            recycleList.also { view ->
                view.loadLinear(
                    refresh = swipeRefresh,
                    onRefresh = {
                        getTotalData()
                        getData(0)
                    },
                    onLoad = {
                        if (!isLoadingMore) {
                            isLoadingMore = true
                            getData(pageNum)
                        }
                    }
                )

                view.addItemDecoration(SpaceTBDecoration())

                mAdapter = SlimAdapter.creator()
                    .register<ManagerStation>(R.layout.item_station_list) { jector, bean, _ ->
                        jector.apply {
                            text(R.id.item_station_title, bean.name)
                            text(R.id.item_station_name, bean.linkman.orEmpty("无"))
                            text(R.id.item_station_phone, bean.phone)
                            text(R.id.item_station_sale, bean.subQuantity)
                            text(R.id.item_station_amount, bean.subPrice.formatDecimal())

                            with<ImageView>(R.id.item_station_img) {
                                it.loadCircleImage(bean.logo, R.mipmap.default_logo)
                            }

                            clicked {
                                startActivityEx<ReportStationDetailActivity>(
                                    "memberId" to bean.member,
                                    "logo" to bean.logo.orEmpty(),
                                    "title" to bean.name.orEmpty(),
                                    "name" to bean.linkman.orEmpty("无"),
                                    "phone" to bean.phone.orEmpty()
                                )
                            }
                        }
                    }
                    .attachTo(view)
            }
        }

        tvRight?.oneClick { text ->
            text as TextView

            showActionDialog(
                "近7天", "本月"
            ) { index, hint ->
                text.hint = hint

                when (index) {
                    0 -> {
                        mBeginDate = getBeforeDate(6)
                        mEndDate = getTodayDate()
                    }
                    1 -> {
                        mBeginDate = getFirstDate()
                        mEndDate = getEndDate()
                    }
                }

                getTotalData()
                updateList()
            }
        }
    }

    /**
     * 获取水站总数
     */
    private fun getTotalData() {
        getViewModel()
            .request(
                isLoadingBefore = false,
                isLoadingAfter = false,
                requestUrl = ReportUrl.getBaseUrl().zStasum,
                requestMethod = HttpMethod.GET,
                params = arrayOf(
                    "beginDate" to mBeginDate,
                    "endDate" to mEndDate
                ),
                onSuccess = {
                    val obj = JSONObject(it)

                    mBinding
                        .stationTotal
                        .text = obj.optNotNull("n1")
                }
            )
    }

    /**
     * 水站列表
     */
    override fun getData(index: Int, isLoading: Boolean) {
        getViewModel()
            .stationList(
                page = index,
                beginDate = mBeginDate,
                endDate = mEndDate,
                onSuccess = {
                    mList.apply {
                        if (index == 0) {
                            clear()
                            pageNum = 0
                        }
                        addItems(it)
                        if (it.count() > 0) pageNum++
                    }

                    mAdapter.setDataList(mList)
                },
                onFinally = {
                    isLoadingMore = false

                    mBinding
                        .listLayout
                        .apply {
                            swipeRefresh.isRefreshing = false

                            emptyLayout
                                .emptyView
                                .isVisible = mList.isEmpty()
                        }
                }
            )
    }

    /**
     * 刷新列表数据
     */
    private fun updateList() {
        mBinding
            .listLayout
            .apply {
                swipeRefresh.isRefreshing = true

                emptyLayout
                    .emptyView
                    .gone()
            }

        if (mList.isNotEmpty()) {
            mList.clear()
            mAdapter.notifyDataSetChanged()
        }

        getData(0)
    }

}